package cn.tedu.qxhouserent.house.controller;

import cn.tedu.qxhouserent.commons.pojo.vo.PageData;
import cn.tedu.qxhouserent.house.pojo.param.SectionAddNewParam;
import cn.tedu.qxhouserent.house.pojo.param.SectionUpdateInfoParam;
import cn.tedu.qxhouserent.house.pojo.vo.*;
import cn.tedu.qxhouserent.house.service.SectionService;
import cn.tedu.qxhouserent.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/section")
@Api(tags = "区域管理模块")
@Slf4j
@Validated
public class SectionController {


    @Autowired
    SectionService sectionService;

    @PostMapping("/add-new")
    @ApiOperation("添加区域")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/pms/section/add-new')")
    public JsonResult addNew(@Valid SectionAddNewParam sectionAddNewParam){
        log.debug("开始处理添加区域业务");
        System.out.println(sectionAddNewParam);

        sectionService.addNew(sectionAddNewParam);

        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("根据ID删除区域")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "区域ID", required = true, dataType = "long")
    })
    @PreAuthorize("hasAuthority('/pms/section/delete')")
    public JsonResult deleteById(@Range(min = 1, message = "请提交合法的ID值！")
                                 @RequestParam Long id) {
        log.debug("开始处理【根据ID删除区域】的请求，参数：{}", id);
        sectionService.deleteById(id);
        log.debug("处理【根据ID删除区域】完毕！");
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改区域详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "区域ID", required = true, dataType = "long")
    })
    @PreAuthorize("hasAuthority('/pms/section/update')")
    public JsonResult updateById(@RequestParam @Range(min = 1, message = "请提交有效的ID值！") Long id,
                                 @Valid SectionUpdateInfoParam sectionUpdateInfoParam) {
        log.debug("开始处理【修改区域】的业务，参数：{}", sectionUpdateInfoParam);
        sectionService.updateById(id, sectionUpdateInfoParam);
        log.debug("处理【修改区域】完毕！");
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/display")
    @ApiOperation("显示区域")
    @ApiOperationSupport(order = 312)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "区域ID", required = true, dataType = "long")
    })
    @PreAuthorize("hasAuthority('/pms/section/update')")
    public JsonResult setDisplay(@PathVariable @Range(min = 1, message = "请提交有效的区域ID值！") Long id) {
        log.debug("开始处理【显示区域】的请求，参数：{}", id);
        sectionService.setDisplay(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/hidden")
    @ApiOperation("隐藏区域")
    @ApiOperationSupport(order = 313)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "区域ID", required = true, dataType = "long")
    })
    @PreAuthorize("hasAuthority('/pms/section/update')")
    public JsonResult setHidden(@PathVariable @Range(min = 1, message = "请提交有效的区域ID值！") Long id) {
        log.debug("开始处理【隐藏区域】的请求，参数：{}", id);
        sectionService.setHidden(id);
        return JsonResult.ok();
    }

    @GetMapping("/standard")
    @ApiOperation("根据ID查询区域")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "区域ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "请提交合法的ID值！")
                                      @RequestParam Long id) {
        log.debug("开始处理【根据ID查询区域】的请求，参数：{}", id);
        SectionStandardVO sectionStandardVO = sectionService.getStandardById(id);
        log.debug("处理【根据ID查询区域】完毕");
        return JsonResult.ok(sectionStandardVO);
    }

    @GetMapping("/list-by-parent")
    @ApiOperation("查询区域列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父级类Id", paramType = "query", dataType = "long",required = true),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "integer",defaultValue = "1")
    })
    public JsonResult list(@Range(message = "请提交有效的父级类别ID值！") Long parentId,
            @Range(min = 1, message = "请提供正确的页码值！") Integer page,String query) {
        log.debug("开始处理【根据父级区域查询子级区域列表】的请求，父级类别：{}，页码：{}", parentId, page);
        Integer pageNum = page == null ? 1 : page;
        PageData<SectionListItemVO> pageData;
        if ("all".equals(query)) {
            pageData = sectionService.listByParentId(parentId, pageNum, Integer.MAX_VALUE);
        } else {
            pageData = sectionService.listByParentId(parentId, pageNum);
        }
        return JsonResult.ok(pageData);
    }

    @GetMapping("/tree")
    @ApiOperation("查询区域树")
    @ApiOperationSupport(order = 411)
    public JsonResult<List<SectionTreeItemVO>> listTree() {
        log.debug("开始处理【获取区域树】的业务，参数：无");
        List<SectionTreeItemVO> sectionTree = sectionService.listTree();
        return JsonResult.ok(sectionTree);
    }

}
